const express= require('express');
const app = express()

app.use('/uploads',express.static('uploads'))


/* 引入https */
var http = require('http');
// const https = require('https');
const fs = require('fs');

const router = require('./router/router')
var  cors = require('cors'); 

/* token校验 */
const interceptor=require( './middleware/Interceptor' );
var bodyParser = require('body-parser')

/* 防止post空对象校验 */
const prevent=require( './middleware/preventNull' );
const { result, result_err } = require('./util/result/result.js');

var privateKey  = fs.readFileSync('./cert/runto2.com.key', 'utf8');
var certificate = fs.readFileSync('./cert/runto2.com_bundle.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
// const routes = require('./routes');








// const asyncHandler = require('express-async-handler')

//轻量云部署端口

// 本地端口4000
 const port = 4000
//  const httpsport = 443;







// const jwt=require( './util/token/jwt' );







/* 跨域解决-写在注册路由的前面*/
app.use(cors()); 
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())



/* 查看前端传来的内容的中间件 */
// app.use((req,res,next) => {
//   if(req.method==='GET'){
//     console.log('get请求，前端数据：',req.query);
//   }else if(req.method==='POST'){
//     console.log('post请求，前端数据：',req.body);
//   }
//   next()
// })

/* 防止post里空对象 */
// app.use(prevent)


/* 随机生成匿名用户ID */
// app.use((req,res,next) => {
//   next();
// })

/* token校验 */
// app.use(interceptor)

/* 路由列表 */

// app.use(router
  
  

/* 

routes(app);

*/
//   )
app.use(router)


/* 错误级别 */
app.use(
    (err,req,res,next) => {
    if(err){
      console.log('系统级错误',err);
      res.send(result_err(err.message))
      return
    }
    }
  )

var httpServer = http.createServer(app); 
// var httpsServer = https.createServer(credentials, app);
// httpServer.listen(port);

httpServer.listen(4000, '192.168.10.88', () => {
  console.log('Server running on http://192.168.10.88:4000');
});
// httpsServer.listen(httpsport);

// app.listen(port, () => {
//   console.log(`Example app listening at http://localhost:${port}`)
// })



// app.listen(httpsport, () => {
//   console.log(`Example app listening at http://localhost:${httpsport}`)
// })








